<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>迭代器模式</title>
</head>
<body>
    <script>
        // 发布订阅模式
        var Event = function() {
            this.eventMap = {}
        }
        // 监听
        Event.prototype.on = function(key, fn) {
            this.eventMap[key] = this.eventMap[key] || []
            this.eventMap[key].push(fn)
            return true
        }
        // 移出监听
        Event.prototype.remove = function(key, fn) {
            this.eventMap[key] = this.eventMap[key] || []
            let i = this.eventMap[key].indexOf(fn)
            this.eventMap[key].splice(i, 1);
        }
        // 触发
        Event.prototype.emit = function() {
            var key = Array.prototype.shift.call(arguments)
            if (Array.isArray(this.eventMap[key])) {
                for (var fn of this.eventMap[key]) {
                    fn.apply(this, arguments)
                }
            }
        }


        var event = new Event();

        var f1 = function() {
            console.log('获取用户会员信息')
        }

        var f2 = function() {
            console.log('获取用户会员信息', )
        }

        event.on('login', f1)
        event.on('login', f2)

        event.remove('login', f2)

        event.emit('login', 1,2,3,44)


    </script>
</body>
</html>